<%@ taglib prefix="diplay" uri="http://www.springframework.org/tags/form" %>
<%--
  Created by IntelliJ IDEA.
  User: hys
  Date: 2020/2/22
  Time: 15:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>网络笔记注册</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/layui/css/layui.css">
    <script src="${pageContext.request.contextPath}/static/js/jquery-3.2.1.js"></script>
    <script src="${pageContext.request.contextPath}/layui/layui.all.js"></script>
    <style type="text/css">
        .linear{
            background-image: linear-gradient(to right, #eea2a2 0%, #bbc1bf 19%, #57c6e1 42%, #b49fda 79%, #7ac5d8 100%);
        }
    </style>
</head>
<body class="linear">
    <div class="layui-container" style="top: 100px">
        <div class="layui-row">
            <div class="layui-col-md12 layui-hide-xs xs-hide"></div>
            <div class="layui-col-md4 layui-col-md-offset4">
                <div style="text-align: center">
                    <h2>注册</h2>
                </div>
            </div>
        </div>
        <div class="layui-container" style="top: 50px">
            <div class="layui-row">
                <div class="layui-col-md12 layui-hide-xs xs-hide"></div>
                <div class="layui-col-md4 layui-col-md-offset4">
                    <div class="main formbox">
                        <form method="post" accept-charset="utf-8" class="layui-form" id="userForm">
                            <div class="layui-form-item">
                                <label class="layui-form-label">登录名</label>
                                <div class="layui-input-block">
                                    <input type="text" id="loginname" name="loginname" required lay-verify="required"
                                           placeholder="请输入登录名" autocomplete="off" class="layui-input"><span id="namemsg"></span>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">请输入邮箱</label>
                                <div class="layui-input-block">
                                    <input type="email" id="email" name="email" required lay-verify="required" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">输入验证码</label>
                                <div class="layui-input-block">
                                    <input type="text" id="vcode" name="vcode" required lay-verify="required" placeholder="输入验证码"
                                           autocomplete="off" class="layui-input layui-input-inline" style="width: 50%">
                                    <input class="layui-btn layui-btn-normal layui-inline" id="btn" name="btn" value="发送验证码" readonly="readonly" onclick="sendMessage()" style="width: 40%"/>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">请输入密码</label>
                                <div class="layui-input-block">
                                    <input type="password" id="password" name="password" required lay-verify="required"
                                           placeholder="请输入密码" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">请确认密码</label>
                                <div class="layui-input-block">
                                    <input type="password" id="repass" name="repass" required lay-verify="required"
                                           placeholder="请确认密码" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <label class="layui-form-label">昵称</label>
                                <div class="layui-input-block">
                                    <input type="text" id="nickname" name="nickname" required lay-verify="required"
                                           placeholder="请输入昵称" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <input type="hidden" id="avatar" name="avatar" value="" class="image">
                            <div class="layui-form-item">
                                <label class="layui-form-label">头像:</label>
                                <div class="layui-upload">
                                    <button type="button" class="layui-btn" id="test1" style="float:left; display:inline">上传图片</button>
                                    <div class="layui-upload-list">
                                        <img class="layui-upload-img" id="demo1" width="35%" style="float:left; display:inline">
                                        <p id="demoText"></p>
                                    </div>
                                </div>
                            </div>
                        </form>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" onclick="register()">立即提交</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </div>
                        <input type="hidden" id="ecode" name="ecode" value="ecode"/>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
<script>
    $(function () {
       $("#loginname").blur(function () {
            var loginname=$(this).val();
           if (loginname == "") {
               alert("请输入登录名");
               return false;
           }
            $.ajax({
               type: "post",
                url: "${pageContext.request.contextPath}/user/checkname",
                data: "loginname="+loginname,
                success:function (codes) {
                    //alert(codes);
                    if (codes=="001"){
                        $("#namemsg").text("用户名可用").css("color","green").css("font-size","10px");
                    }else{
                        $("#namemsg").text("用户名已存在").css("color","red").css("font-size","10px");
                        return false;
                    }

                }
            });
       });
    });

    $(function () {
        $("#btn").click(function () {
            var email=$("#email").val();
            $.ajax({
                type: "post",
                url: "${pageContext.request.contextPath}/user/emailcod",
                data: "email="+email,
                success:function (codes) {
                    document.getElementById("btn").value="已发送";
                    // $("#btn").attr("value","已发送");
                    $("#ecode").attr("value",codes);
                }
            });
        });
    });

    layui.use('upload', function(){
        var $ = layui.jquery
            ,upload = layui.upload;

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: '${pageContext.request.contextPath}/user/upload'
            ,accept:'images'
            ,size:50000
            ,before: function(obj){

                obj.preview(function(index, file, result){

                    $('#demo1').attr('src', result);
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg('上传失败');
                }
                //上传成功
                var demoText = $('#demoText');
                demoText.html('<span style="color: #4cae4c;">上传成功</span>');

                var fileupload = $("#avatar");
                fileupload.attr("value",res.data.src);
                console.log(fileupload.attr("value"));
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });


    });

    
    function register() {
        var loginname = $("#loginname").val();
        var password = $("#password").val();
        var repass = $("#repass").val();
        var email=$("#email").val();
        var nickname=$("#nickname").val();
        var vcode=$("#vcode").val();
        var ecode=$("#ecode").val();
        var avatar=$("#avatar").val();
        // var userData = $("#userForm").serialize();
        if (password == "") {
            alert("请输入密码！");
            return false;
        }
        if (password != repass) {
            alert("两次密码不一致！");
            return false;
        }
        if (avatar == "") {
            alert("上传头像！");
            return false;
        }
        $.ajax({
            url :  "${pageContext.servletContext.contextPath}/user/register",
            data:{"loginname":loginname,"password":password,"email":email,"nickname":nickname,"avatar":avatar,"vcode":vcode,"ecode":ecode},
            // data : userData,
            type : "post",
            success : function (msg) {
                // alert(msg);
                console.log(msg);
                if (msg == 'success') {
                    //跳转到登录
                    alert("注册成功");
                    window.location.href = "${pageContext.servletContext.contextPath}/user/tologin";
                }else {
                    alert("验证码有误");
                }
            }
        });
    }

</script>
</html>
